package com.crane.oss.school.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crane.common.core.exception.AppException;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.exception.SysException;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.enums.GradeEnum;
import com.crane.oss.enums.SexEnum;
import com.crane.oss.school.bean.request.QueryStudentChangeClassReq;
import com.crane.oss.school.bean.request.studentchangeclass.AddStudentChangeClassReq;
import com.crane.oss.school.bean.request.studentchangeclass.UpdateStudentChangeClassReq;
import com.crane.oss.school.bean.resp.StudentChangeClassResp;
import com.crane.oss.school.dao.StudentChangeClassDao;
import com.crane.oss.school.model.StudentChangeClass;
import com.crane.oss.school.service.StudentChangeClassService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author zhenxing.pi
 * @email ccsu123456@qq.com
 * @date 2022年03月26日 14时24分54秒
 */
@Service
@Slf4j
public class StudentChangeClassServiceImpl implements StudentChangeClassService {

    @Resource
    protected StudentChangeClassDao studentChangeClassDao;

    @Override
    public StudentChangeClassResp queryStudentChangeClass(Long id) {
        log.info("StudentChangeClassBOImpl queryStudentChangeClass id={}", id);
        StudentChangeClass studentChangeClass = studentChangeClassDao.selectById(id);
        if (null == studentChangeClass) {
            throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "studentChangeClass");
        }
        StudentChangeClassResp studentChangeClassResp = new StudentChangeClassResp();
        BeanUtil.copyProperties(studentChangeClass, studentChangeClassResp);
        return studentChangeClassResp;
    }

    @Override
    public IPage<StudentChangeClassResp> queryList(QueryStudentChangeClassReq queryRequest) {
        log.info("StudentChangeClassServiceImpl queryList queryRequest={}", queryRequest);
		Map<String, Object> map = BeanUtil.asMap(queryRequest);
		map.put("offset", queryRequest.getPageSize() * (queryRequest.getPageNo() - 1));
		map.put("limit", queryRequest.getPageSize());
		int total = studentChangeClassDao.queryListTotal(map);

		IPage<StudentChangeClassResp> result = new Page<>();
		result.setCurrent(queryRequest.getPageNo());
		result.setPages(queryRequest.getPageSize());
		List<StudentChangeClassResp> resultList = new ArrayList<>();
		result.setRecords(resultList);
		result.setTotal(total);
		if (total == 0) {
			return result;
		}

		List<StudentChangeClassResp> queryList = studentChangeClassDao.queryList(map);
        
		for (StudentChangeClassResp change : queryList) {
			change.setGradeCN(GradeEnum.getGradeEnum(change.getGrade()));
			change.setSexCN(SexEnum.getRemark(change.getSex()));
			
			resultList.add(change);
		}
		result.setRecords(resultList);
		return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(AddStudentChangeClassReq addRequest) {
        log.info("StudentChangeClassBOImpl save request={}", addRequest);
        StudentChangeClass studentChangeClass = new StudentChangeClass();
        BeanUtil.copyProperties(addRequest, studentChangeClass);
        int i = 0;
        try {
            i = studentChangeClassDao.insert(studentChangeClass);
        } catch (Exception e) {
            log.info("StudentChangeClassBOImpl add e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(UpdateStudentChangeClassReq updateRequest) {
        log.info("StudentChangeClassServiceImpl update request={}", updateRequest);
        StudentChangeClass studentChangeClass = new StudentChangeClass();
        BeanUtil.copyProperties(updateRequest, studentChangeClass);
        int i = 0;
        try {
            i = studentChangeClassDao.updateById(studentChangeClass);
        } catch (Exception e) {
            log.info("StudentChangeClassServiceImpl add e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id) {
        log.info("StudentChangeClassServiceImpl delete id={}", id);
        return studentChangeClassDao.deleteById(id);
    }

}
